#include "stdafx.h"
#include "QQManagerServer.h"
#include <atlconv.h>
#include "../tinyxml/tinyxml2.h"

using namespace std;

CQQManagerServer* CQQManagerServer::_ManagerServer = NULL;


CQQManagerServer::CQQManagerServer(void):_nGoodsInterval(6 * 1000),_nQQWndInterval(2 * 1000),_bStop(false)
{
	_QQController.LoginQQ(324791644,"");

	LoadConfigXml();
	LoadConfigXml2();
}


CQQManagerServer::~CQQManagerServer(void)
{
}


CQQManagerServer* CQQManagerServer::Ins()
{
	if(_ManagerServer)
		return _ManagerServer;
	return _ManagerServer = new CQQManagerServer();
}


// QQData* GetQQData(QQNUM);
// //	QQData* GetAllQQData(QQNUM);
// 
// std::vector<QQNUM>* GetAllQQNum();




int CQQManagerServer::AllQQSendMSG(GOODS_ID goodid)
{

	return 0;
}


int CQQManagerServer::SendMSG(QQData* qqdata,GOODS_ID goodid)
{
//	qqdata->
//	_QQController.AttachQQ(qqdata);
//	_QQController.AllGroupSendMsg(goodid);
//	qqdata->FirstQQHWND();
	return 0;
}


int CQQManagerServer::SendMSG(HWND hwnd,GOODS_ID goodid)
{
	bool success = _QQController.SendMsg(hwnd,goodid);
	return success;

	map<QQNUM,QQData*>::iterator iter1 = _QQsData.begin();
	map<QQNUM,QQData*>::iterator iter2 = _QQsData.end();

	while (iter1 != iter2)
	{
		bool exist = iter1->second->FindQQHWND(hwnd);
		if(exist)
		{
			bool success = _QQController.SendMsg(hwnd,goodid);
			if(success)
				//AddGoodsToSentQueue(hwnd,goodid);
					;
			break;
		}
	}
	return 1;
}


void CQQManagerServer::AddGoodsToWaitQueue(GoodsInfo*)
{

}


void CQQManagerServer::RemoveGoodsFromWaitQueue(GoodsInfo*)
{

}


GoodsInfo* CQQManagerServer::GetGoodsInfo(GOODS_ID)
{
	return new GoodsInfo();
}


bool CQQManagerServer::LoadConfigXml()
{
	wchar_t* wszFullPath;
	_get_wpgmptr(&wszFullPath);
	wchar_t wszTemp[] = _T("\\");
	wchar_t* wszEnd = StrRChrW(wszFullPath,NULL,*wszTemp);

	wchar_t wszXmlFullPath[MAX_PATH] = {0};
	int len = wszEnd - wszFullPath + 1;
	wcsncpy_s(wszXmlFullPath,MAX_PATH,wszFullPath,len);
	wcscat_s(wszXmlFullPath,_T("QQAgentConfig.xml"));

	USES_CONVERSION;
	string strXmlFullPath = W2A(wszXmlFullPath);

	tinyxml2::XMLDocument  xmlDoc;
	if(xmlDoc.LoadFile(strXmlFullPath.c_str()) != tinyxml2::XML_SUCCESS)
		return false;

	tinyxml2::XMLElement* xmlRoot = NULL;
	xmlRoot = xmlDoc.FirstChildElement("QQConfig");
	if(!xmlRoot)
		return false;

	tinyxml2::XMLElement* xmlElemQQ = xmlRoot->FirstChildElement("QQ");
	while (xmlElemQQ)
	{
		QQConfig QQCg;
		QQCg.num = xmlElemQQ->IntAttribute("num");
		QQCg.password = xmlElemQQ->Attribute("password");
		tinyxml2::XMLElement* xmlElemQQWnd = xmlElemQQ->FirstChildElement();

		while (xmlElemQQWnd)
		{
			string strTemp = xmlElemQQWnd->Attribute("name");
			QQCg.wnds.push_back(strTemp);
			xmlElemQQWnd = xmlElemQQWnd->NextSiblingElement();
		}

		_QQConfigs.push_back(QQCg);
		xmlElemQQ = xmlElemQQ->NextSiblingElement();
	}
	return true;
}


bool CQQManagerServer::LoadConfigXml2()
{
	wchar_t* wszFullPath;
	_get_wpgmptr(&wszFullPath);
	wchar_t wszTemp[] = _T("\\");
	wchar_t* wszEnd = StrRChrW(wszFullPath,NULL,*wszTemp);

	wchar_t wszXmlFullPath[MAX_PATH] = {0};
	int len = wszEnd - wszFullPath + 1;
	wcsncpy_s(wszXmlFullPath,MAX_PATH,wszFullPath,len);
	wcscat_s(wszXmlFullPath,_T("QQAgentConfig2.xml"));

	USES_CONVERSION;
	string strXmlFullPath = W2A(wszXmlFullPath);

	tinyxml2::XMLDocument  xmlDoc;
	if(xmlDoc.LoadFile(strXmlFullPath.c_str()) != tinyxml2::XML_SUCCESS)
		return false;

	tinyxml2::XMLElement* xmlRoot = NULL;
	xmlRoot = xmlDoc.FirstChildElement("QQConfig");
	if(!xmlRoot)
		return false;

	tinyxml2::XMLElement* xmlElemQQWnd = xmlRoot->FirstChildElement("QQWnd");
	while (xmlElemQQWnd)
	{
		string strTemp = xmlElemQQWnd->Attribute("name");

		_QQConfigs2.wnds.push_back(strTemp);
		xmlElemQQWnd = xmlElemQQWnd->NextSiblingElement();
	}
	return true;
}


void CQQManagerServer::RunConfig()
{
	if(_QQConfigs.empty())
		return;

	for (QQConfig& QQCg:_QQConfigs)
	{
		_QQController.LoginQQ(QQCg.num,QQCg.password);
	}
}


void CQQManagerServer::RunConfig2()
{
	return;
	while (!_bStop)
	{
		Sleep(_nGoodsInterval);

		if(_QQConfigs2.wnds.empty())
			continue;

		_WaitForSendGoodsQueue.push(1001);
		if(_WaitForSendGoodsQueue.empty())
			continue;


		GOODS_ID GoodsId = _WaitForSendGoodsQueue.front();
		_WaitForSendGoodsQueue.pop();

		wstring wstrWndName;
		HWND QQhwnd = NULL;
		for (string& strWndName:_QQConfigs2.wnds)
		{
			USES_CONVERSION;
			wstrWndName = A2W(strWndName.c_str());
			QQhwnd = FindWindowW(cQQClassName.c_str(),wstrWndName.c_str());

			if(QQhwnd)
			{
				SendMSG(QQhwnd,GoodsId);
				Sleep(_nQQWndInterval);
			}
		}
	}
}


void CQQManagerServer::InitQQWnd2()
{
	if(_QQConfigs2.wnds.empty())
		return;

	wstring wstrWndName;
	HWND QQhwnd = NULL;
	for (string& strWndName:_QQConfigs2.wnds)
	{
		USES_CONVERSION;
		wstrWndName = A2W(strWndName.c_str());
		QQhwnd = FindWindowW(cQQClassName.c_str(),wstrWndName.c_str());

		if(QQhwnd)
			SendMSG(QQhwnd,3);
	}
	
}


void CQQManagerServer::PasreNetData()
{

}


void CQQManagerServer::OnRecvData(char * ch,int )
{

}